package com.psp.park.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.northend.util.AppTextUtil;
import com.northend.util.StringUtil;
import com.psp.park.constants.ConfigConstants;
import com.psp.park.controller.res.bean.RCooperationBean;
import com.psp.park.controller.res.bean.RUserBean;
import com.psp.park.model.CompanyCategoryRelationBean;
import com.psp.park.model.CooperationBean;
import com.psp.park.model.ParkUserBean;
import com.psp.park.persist.dao.CooperationDao;
import com.psp.park.service.CooperationService;
import com.psp.park.service.exception.ServiceException;

@Service
public class CooperationServiceImpl implements CooperationService {
	
	@Autowired
	CooperationDao cooperationImpl;


	@Override
	public PageResult<RCooperationBean> getList(int page, int pageSize, String puid, int cateCode, String content, ParkUserBean user, int handleState) {
		PageResult<RCooperationBean> result = new PageResult<RCooperationBean>();
		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}
		String qinuUrl = ConfigConstants.getInstance().getUrl("qinuUrl");
		int count = cooperationImpl.selectCount(puid, cateCode, content);
		List<CooperationBean> resList = cooperationImpl.selectList(page, pageSize, puid, cateCode, content);
		List<RCooperationBean> resData = new ArrayList<>();
		for(CooperationBean co : resList) {
			RCooperationBean rc = new RCooperationBean();
			rc.setBrief(co.getBrief());
			rc.setCharterImg(qinuUrl + co.getCharterImg());
			rc.setCid(co.getCid());
			rc.setCreateTime(co.getCreateTime().getTime());
			rc.setEmail(co.getEmail());
			rc.setFile(qinuUrl + co.getFile());
			rc.setName(co.getName());
			rc.setPhone(co.getPhone());
			rc.setUserName(co.getUserName());
			rc.setHandleState(co.getHandleState());
			ParkUserBean pu = co.getUser();
			RUserBean ruser = new RUserBean();
			ruser.setAccount(pu.getAccount());
			ruser.setUserName(pu.getNickName());
			ruser.setHeadIcon(pu.getHeadIcon());
			ruser.setPark(pu.getPark());
			rc.setUser(ruser);
			if(co.getCates()!=null && co.getCates().size() > 0) {
				String cates = "[";
				for(CompanyCategoryRelationBean ccr : co.getCates()) {
					cates +=  "'" + ccr.getCateCode() + "',";
				}
				cates = cates.substring(0, cates.length()-1) + "]";
				rc.setServiceCate(cates);
			}
			resData.add(rc);
		}
		result.setCount(count);
		result.setData(resData);
		return result;
	}
	
	@Transactional(rollbackFor = Exception.class)
	@Override
	public boolean EditInfo(String puid, String cid, String name, String brief, String charterImg, String file,
			String userName, String phone, String email, String cateCode) {
		boolean flag = false;
		boolean isNew = StringUtil.isEmpty(cid);

		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}
		CooperationBean bean = new CooperationBean();
		bean.setBrief(brief);
		bean.setCharterImg(charterImg);
		bean.setEmail(email);
		bean.setFile(file);
		bean.setName(name);
		bean.setPhone(phone);
		bean.setPid(puid);
		bean.setUserName(userName);
		
		if (isNew) {
			cid = AppTextUtil.getPrimaryKey();
			bean.setCid(cid);
			bean.setHandleState(0);//未处理
			bean.setCreateTime(new Timestamp(new Date().getTime()));
			if(!StringUtil.isEmpty(cateCode)) {
				String[] cates = cateCode.split(",");
				flag = cooperationImpl.insertCooperateCateRelation(cid, cates) > 0;
				if(!flag) {
					throw new ServiceException("cooperation_cate_relation_error");
				}
			}
			flag = cooperationImpl.insert(bean) > 0;
		} else {
			bean.setCid(cid);
			if(!StringUtil.isEmpty(cateCode)) {
				flag = cooperationImpl.delCooperateCateRelation(cid) > 0;
				if(!flag) {
					throw new ServiceException("cooperation_cate_relation_del_error");
				}
				String[] cates = cateCode.split(",");
				flag = cooperationImpl.insertCooperateCateRelation(cid, cates) > 0;
				if(!flag) {
					throw new ServiceException("cooperation_cate_relation_error");
				}
			}
			flag = cooperationImpl.update(bean) > 0;
		}

		if (!flag) {
			throw new ServiceException("fail");
		}
		return flag;
	}

	@Override
	public int getNum(String puid) {
		return  cooperationImpl.selectCount(puid, 0, null);
	}

	@Override
	public boolean deleteById(String puid, String cid) {
		if (StringUtil.isEmpty(puid)) {
			throw new ServiceException("token_fail");
		}
		List<String> cids = JSON.parseArray(cid, String.class);
		return cooperationImpl.deleteById(cids) > 0;
	}

}
